package com.itheima.sfbx.insurance.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.sfbx.framework.commons.utils.BeanConv;
import com.itheima.sfbx.insurance.dto.SafeguardVO;
import com.itheima.sfbx.insurance.mapper.SafeguardMapper;
import com.itheima.sfbx.insurance.pojo.CategorySafeguard;
import com.itheima.sfbx.insurance.pojo.PlanSafeguard;
import com.itheima.sfbx.insurance.pojo.Safeguard;
import com.itheima.sfbx.insurance.service.ICategorySafeguardService;
import com.itheima.sfbx.insurance.service.IPlanSafeguardService;
import com.itheima.sfbx.insurance.service.ISafeguardService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;
/**
 * @Description：保障项服务实现类
 */
@Slf4j
@Service
public class SafeguardServiceImpl extends ServiceImpl<SafeguardMapper, Safeguard> implements ISafeguardService {



    @Override
    public Page<SafeguardVO> findPage(SafeguardVO safeguardVO, int pageNum, int pageSize) {
        Page<Safeguard> page = this.lambdaQuery()
                .eq(StrUtil.isNotBlank(safeguardVO.getSafeguardKey()), Safeguard::getSafeguardKey, safeguardVO.getSafeguardKey())
                .like(StrUtil.isNotBlank(safeguardVO.getSafeguardKeyName()), Safeguard::getSafeguardKeyName, safeguardVO.getSafeguardKeyName())
                .eq(StrUtil.isNotBlank(safeguardVO.getDataState()), Safeguard::getDataState, safeguardVO.getDataState())
                .page(new Page<>(pageNum, pageSize));
        return BeanConv.toPage(page, SafeguardVO.class);
    }

    @Override
    public SafeguardVO findById(String safeguardId) {
        //TODO
        return null;
    }

    @Override
    @Transactional
    public SafeguardVO save(SafeguardVO safeguardVO) {
        Integer count = this.lambdaQuery().eq(Safeguard::getSafeguardKey, safeguardVO.getSafeguardKey()).count();
        if (count>0){
            throw new RuntimeException("保障项存在，不能重复添加");
        }
        Safeguard safeguard = BeanConv.toBean(safeguardVO, Safeguard.class);
        boolean save = this.save(safeguard);
        if (!save){
            throw new RuntimeException("操作失败");
        }
        return BeanConv.toBean(safeguard, SafeguardVO.class);
    }

    @Autowired
    private ICategorySafeguardService categorySafeguardService;

    @Autowired
    private IPlanSafeguardService planSafeguardService;

    @Override
    @Transactional
    public Boolean update(SafeguardVO safeguardVO) {
        Safeguard safeguard1 = this.getById(safeguardVO.getId());
        if (safeguard1==null){
            throw new RuntimeException("保障项不存在，无法修改");
        }
        Integer count1 = categorySafeguardService.lambdaQuery()
                .eq(CategorySafeguard::getSafeguardKey, safeguardVO.getSafeguardKey())
                .count();
        Integer count2 = planSafeguardService.lambdaQuery()
                .eq(PlanSafeguard::getSafeguardKey, safeguardVO.getSafeguardKey())
                .count();
        if (count1>0 || count2>0){
            throw new RuntimeException("保障项存在绑定关系，无法修改");
        }
        Safeguard safeguard = BeanConv.toBean(safeguardVO, Safeguard.class);
        boolean update = this.updateById(safeguard);
        if (!update){
            throw new RuntimeException("操作失败");
        }
        return true;
    }

    @Override
    @Transactional
    public Boolean delete(String[] checkedIds) {
        for (String checkedId : checkedIds) {
            Safeguard safeguard = this.getById(checkedId);
            if (safeguard==null){
                throw new RuntimeException("保障项不存在，删除失败");
            }
            Integer count = planSafeguardService.lambdaQuery()
                    .eq(PlanSafeguard::getSafeguardKey, safeguard.getSafeguardKey())
                    .count();
            Integer count1 = categorySafeguardService.lambdaQuery()
                    .eq(CategorySafeguard::getSafeguardKey, safeguard.getSafeguardKey())
                    .count();
            if (count1>0 || count>0){
                throw new RuntimeException("保障项存在绑定关系，无法删除");
            }
        }
        return this.removeByIds(Arrays.asList(checkedIds));
    }

    @Override
    public List<SafeguardVO> findList(SafeguardVO safeguardVO) {
        //TODO
        return null;
    }


    @Override
    public List<SafeguardVO> findShowPageItemByKey(List<String> safeguardKeyList) {
        //TODO
        return null;
    }

    @Override
    public SafeguardVO findBySafeguardKey(String safeguardKey) {
        //TODO
        return null;
    }
}
